package com.ls.fw.office.poi.excel.handler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;


/**
 * 
 * @author lisheng
 * @date 2015年10月8日 下午9:59:37
 * @version V1.0
 */
public class ExcelHandler extends ExcelHandlerAdapter {

	
	public ExcelHandler(InputStream is) {
		super(is);
	}
	public ExcelHandler(String path, boolean read) {
		super(path, read);
	}
	public ExcelHandler(String path){
		 super(path);
	}
	public ExcelHandler(OutputStream out){
		 super(out);
	}
	@Override
	public Workbook createWorkbook(){
		try {
			if(StringUtils.isNotBlank(this.getPath())){
				File file = new File(this.getPath());
				if(read){
					return this.createWorkbook(new FileInputStream(new File(this.getPath())));
				}else{
					if(!file.exists()){
						if(!file.getParentFile().exists()){
							file.getParentFile().mkdirs();	
						}
						file.createNewFile();
						Workbook wb = new HSSFWorkbook();
						wb.createSheet();
						return wb;   // OR XSSFWorkbook
					}
				}
			}
		} catch (InvalidFormatException e) {
			throw new IllegalArgumentException(e.getMessage(),e);
		} catch (FileNotFoundException e) {
			throw new IllegalArgumentException(e.getMessage(),e);
		} catch (IOException e) {
			throw new IllegalArgumentException(e.getMessage(),e);
		}
		Workbook wb = new HSSFWorkbook();
		wb.createSheet();
		return wb;
	}
}
